package com.example.j280_webproject.day036;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

public class JDBC02_Register {
    public JDBC02_Register() {
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入用户名：");
        String s_username = scanner.next();
        System.out.println("请输入密码：");
        String s_password = scanner.next();

        JDBC02_Register jdbc_register = new JDBC02_Register();
        try {
            boolean action = jdbc_register.register(s_username,s_password);
            if (action){
                System.out.println("恭喜您！注册成功！");
            }
            else {
                System.out.println("很遗憾！注册失败！");
            }
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
//            System.out.println("系统繁忙请稍后再试！！！");
        }
    }
    public boolean register(String r_username,String r_password) throws ClassNotFoundException, SQLException {
        boolean action = false;
        String driver = "com.mysql.jdbc.Driver";    //固定写法
        Class.forName(driver);                      //通过反射机制 把指定的类加载到当前类中

        //warning
        String url = "jdbc:mysql://localhost:3306/mygxa?useSSL=false&characterEncoding=UTF-8";
        String db_user = "root";
        String db_password = "root";
        Connection conn = DriverManager.getConnection(url, db_user, db_password);
        System.out.println("连接成功");

        //开发自己的数据库操作逻辑代码
        Statement statement = conn.createStatement();
        String sql = "insert into user(username, password, age) values ('"+r_username+"','"+r_password+"',18)";
        statement.execute(sql); //通过jdbc执行一条指定的指令

        conn.close();
        action = true;
        return action;
    }
}
